<?php

class MY_Controller extends CI_Controller {
    protected $is_admin     =   false;
    protected $logged_in    =   false;
    protected $first_name   =   '';
    
    function __construct() {
        parent::__construct();
        
        $this->logged_in    = $this->session->userdata('logged_in');
        $this->is_admin     = $this->session->userdata('is_admin');
        $this->first_name     = $this->session->userdata('first_name');
        
    }
    
    function make_file_name_friendly($file_name) {
        $bad = array_merge(
        array_map('chr', range(0,31)),
        array("<", ">", ":", '"', "/", "\\", "|", "?", "*"," "));
        return str_replace($bad, "_", $file_name);

    }
	
	//Performs a conversion of US date to a date MySQL will understand
	function make_us_date_mysql_friendly($date_passed) {
		$date_var = explode('/',$date_passed);
		
		return $date_var[2].'-'.$date_var[0].'-'.$date_var[1];
	}
	
	//Creates a  thumbnail and smaller image 
	function process_image($uploaded_file, $destination_folder, $height_max = 600, $width_max = 700, $thumbnail_width_max = 150, $contains_full_source_dir = false, $file_prefix = '')
	{
		
            $ret_val	=	'';
		
            $image_source_path = $contains_full_source_dir ? '' : $_SERVER['DOCUMENT_ROOT'].'/uploads/';

		// Create an Image from it so we can do the resize
		$image_source_path_and_file_name = $image_source_path.$uploaded_file;
		$image_target_path =  $_SERVER['DOCUMENT_ROOT'].'/'.$destination_folder.'/';


		$batch_time_stamp = strval(time()).'_'.rand(); //append to the end of all new files created so that they don't overwrite existing files and can keep them toegether.
        
                $file_prefix            = $file_prefix === '' ? $this->session->userdata('member_id') : $file_prefix;
		$new_image_name         = $file_prefix.'_'.$batch_time_stamp.'.jpg';
		$new_image_thumb_name   = str_replace('.jpg', '_thumb.jpg', $new_image_name);

		$src	=	imagecreatefromjpeg($image_source_path_and_file_name);


		// Capture the original size of the uploaded image
		list($width,$height)	=	getimagesize($image_source_path_and_file_name);

		//this should not change based on the high values entered above.  
		//See if adjust height will still meet Width Max.  If not adjust image according to height instead of width
		$new_height	= ($width_max / $width) * $height;
		$no_change	= false;
		if ($width <= $width_max && $height <= $height_max)
		{
		  $no_change = true;
		  $new_width = $width;
		  $new_height = $height;
		}
		elseif ($new_height > $height_max)
		  $new_width = ($height_max / $height) * $width;
		else //Adjust image based on height
		  $new_width = $width_max;

		//echo '<br><b>New Width:</b> $new_width<br>';
		if ($no_change == false)
		{
		  $new_height=($height/$width)*$new_width;
		  $tmp=imagecreatetruecolor($new_width,	$new_height);
		  // this line actually does the image resizing, copying from the original
		  // image into the $tmp image
		  //echo 'Copying image to smaller size...<br>';
		  imagecopyresampled($tmp,$src,0,0,0,0,$new_width,$new_height,$width,$height);
		}
		else
		{
		  //echo 'No change required for image size...<br>';
		  $tmp = imagecreatefromjpeg($image_source_path_and_file_name);
		}

		// now write the resized image to disk. 
		//echo 'writing resized to:  $image_target_path.$new_image_name<br>';
		imagejpeg($tmp,$image_target_path.$new_image_name,100);

		//echo 'Creating image thumbnail...<br>';
		$new_width_thumb = $thumbnail_width_max;
		//echo '$new_height/$new_width)*$new_width_thumb<br>';
		$new_height_thumb =($new_height/$new_width)*$new_width_thumb;
		$tmp_thumb = imagecreatetruecolor($new_width_thumb,$new_height_thumb);
		imagecopyresampled($tmp_thumb,$tmp,0,0,0,0,$new_width_thumb, $new_height_thumb, $new_width, $new_height);
		//echo 'writing thumbnail to:  $image_target_path$new_image_thumb_name<br>';
		imagejpeg($tmp_thumb,$image_target_path.'/thumbnail/'.$new_image_thumb_name,100);

		//echo 'Removing raw photo...<br>';
		imagedestroy($src);
		imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed.


		if (unlink($image_source_path_and_file_name)==false)
		  echo 'Could not delete file:  $image_source_path_and_file_name';

        $ret_val = $new_image_name;
        
		return $ret_val;
	}
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */